<template>
  <div id="mapId"></div>
</template>
<!-- <button @click="exportData">下载</button> -->
<script>
import axios from "axios";
import globmap from "@/util/globmap";
import { bd09togcj02, gcj02tobd09 } from "coordtransform";
export default {
  data() {
    return {
      listData: [],
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      let eleId = document.getElementById("mapId");
      let str = "";
      axios.get("/json/suidao.json").then((res) => {
        console.log(res);
        res.data.Sheet1.forEach((item) => {
          // item.address=globmap.gcj02towgs84(item.lat,item.lat);
          item.address = bd09togcj02(item.lng, item.lat);
          console.log(item.address,11);
          let earth=globmap.gcj02towgs84(item.address[0],item.address[1])
          item.lon = (earth[0]);
          item.lat = (earth[1]);
          console.log(earth,22);
          str += `<Placemark>
		<name>${item.name}</name>
		<styleUrl>Style0</styleUrl>
		<Point>
			<coordinates>${item.lon},${item.lat},0.000000</coordinates>
		</Point>
	</Placemark>`;
        });
        eleId.innerHTML = str;
        console.log(res);
        this.listData = res.data.data;
      });
    },
   
    bd09togcj02(bd_lon, bd_lat) {
      const x_pi = 3.14159265358979324 * 3000.0 / 180.0;
      let z = Math.sqrt(bd_lon * bd_lon + bd_lat * bd_lat) + 0.00002 * Math.sin(bd_lat * x_pi);
      let theta = Math.atan2(bd_lat, bd_lon) + 0.000003 * Math.cos(bd_lon * x_pi);
      let gg_lng = z * Math.cos(theta) + 0.0065;
      let gg_lat = z * Math.sin(theta) + 0.006;
      return [gg_lng, gg_lat];
    },
    // 百度坐标转Google坐标
    bdToGoogle(bdLat, bdLon) {
      axios.get("/json/point.json").then((res) => {
        console.log(res);
        res.data.data.forEach((item) => {
          let newPoint = bd09togcj02(item.lng, item.lat); // 先从百度坐标转到火星坐标
        });
      });
    },
    exportData() {
      // 将数据转换为JSON字符串
      const jsonData = JSON.stringify(this.listData);
      // 创建一个Blob对象，并使用它创建一个链接元素
      const blob = new Blob([jsonData], { type: "application/json" });
      const url = URL.createObjectURL(blob);
      // 创建一个a标签用于触发下载
      const downloadLink = document.createElement("a");
      downloadLink.href = url;
      downloadLink.download = "data.json"; // 下载文件的名称
      document.body.appendChild(downloadLink);
      downloadLink.click();
      document.body.removeChild(downloadLink);
    },
    generateRandomCaptcha(length) {
      const chars = "0123456789ABCDEFGH";
      let code = "";
      for (let i = 0; i < length; i++) {
        code += chars[Math.floor(Math.random() * chars.length)];
      }
      return code;
    },
  },
};
</script>

<style>
</style>